package test.com.sky.JUL;

import ex04.pyrmont.MyLoggerV4Adapter;
import sun.util.logging.resources.logging;
import sun.util.logging.resources.logging_zh_CN;

import java.util.ListResourceBundle;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.*;

/**
 * @author : wushikai
 * <p>
 * date : 2022-05-23
 * 参考:  https://cn.bing.com/search?q=string.format+java&qs=CT&pq=string.format&sk=CT1&sc=8-13&cvid=E73172FD61834931812E902566965143&FORM=QBRE&sp=2c
 */
public class Application {



    private static void test(){
        MyLoggerV4Adapter myLoggerV4Adapter = new MyLoggerV4Adapter(Application.class);
        myLoggerV4Adapter.log("卧槽" ,  2);

    }



    private static  void useJulOrigin(){

        Logger logger = Logger.getLogger(Application.class.getName());
        logger = Logger.getLogger(Application.class.getName());            //mark 看上去同样的代码, 底层走的可不一样 ;
        logger.setLevel(Level.INFO);
        logger.setFilter(new ShitFilter());
        print(logger);
    }


    private static void print( Logger logger ){
        logger.warning("shit ,  我草泥马");

        logger.log(Level.FINE, "hello I am  fine ! ");

//        logger.log(new Level("INFO", 8001, new logging_zh_CN()) ,  "我操{0}" , "尼玛的占位符");
        logger.log(Level.INFO ,  "奶奶的,  定死了占位符位置, {0}" , "我操");


        logging_zh_CN cn = new logging_zh_CN();
        logger.info("  cn.getBaseBundleName()==>          " +cn.getBaseBundleName());
        logger.info("  cn.getString(\"ALL\")==>          " +cn.getString("ALL"));
        logger.info("  cn.getLocale()==>          " +cn.getLocale());
        logger.info("  cn.containsKey(\"ALL\")==>          " +cn.containsKey("ALL"));

        logger.log(Level.INFO,  "我操{0}, 我曰{1}" ,  new String[]{"无情" , "尼玛"});
    }



    private static  void  output2file()   {



        Logger logger = Logger.getLogger(Application.class.getName());

        logger.setUseParentHandlers(false );
        logger.setLevel(Level.FINE);
        try {
            FileHandler fileHandler = new FileHandler((ClassLoader.getSystemClassLoader().getResource("").getPath() +"jul.log.txt").substring(1));
//            FileHandler fileHandler = new FileHandler(  "D:\\jul.log.txt" );
            fileHandler.setLevel(Level.FINE);
            fileHandler.setFormatter(new SimpleFormatter());
            logger.addHandler(fileHandler);
            print(logger);

        }catch (Exception e ) {
            e.printStackTrace();
        }


    }


    private static  void julParentAndSon() {
        // 创建两个 logger 对象，可以认为 logger1 是 logger2 的父亲
        // RootLogger 是所有 logger 对象的顶层 logger，名称默认是一个空的字符串
        Logger logger1 = Logger.getLogger("com.jul");
        Logger logger2 = Logger.getLogger("com.jul.JulTest");

        System.out.println(logger2.getParent() == logger1);
        System.out.println("----");

        System.out.println("logger1名称：" + logger1.getName() +
                "，\n父Logger名称：" + logger1.getParent().getName() +
                "，\n父Logger引用：" + logger1.getParent());
        System.out.println("----");

        System.out.println("logger2名称：" + logger2.getName() +
                "，\n父Logger名称：" + logger2.getParent().getName() +
                "，\n父Logger引用：" + logger2.getParent());
        System.out.println("----");

        // 父亲所做的设置，也能够同时作用于儿子
        // 对 logger1 做日志打印相关的设置，然后我们使用 logger2 进行日志的打印
        logger1.setUseParentHandlers(false);

        ConsoleHandler handler = new ConsoleHandler();
        SimpleFormatter formatter = new SimpleFormatter();
        handler.setFormatter(formatter);
        logger1.addHandler(handler);
        handler.setLevel(Level.ALL);
        logger1.setLevel(Level.ALL);
        logging loggingBundle = new logging();

        ResourceBundle bundle = ListResourceBundle.getBundle("sun.util.logging.resources.logging", Locale.US);
        logger2.setResourceBundle(  bundle );




        //儿子做打印
        logger2.severe("severe：错误信息");
        logger2.warning("warning：警告信息");
        logger2.info("info：默认信息");
        logger2.config("config：配置信息");
        logger2.fine("fine：详细信息(少)");
        logger2.finer("finer：详细信息(中)");
        logger2.finest("finest：详细信息(多)");
    }

    public static void main(String[] args){


        useJulOrigin();



    }

}
